package org.lgiavedoni.android.franq.core.prediction.rule.imp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lombok.Data;

import org.lgiavedoni.android.franq.core.prediction.PredictableEvent;
import org.lgiavedoni.android.franq.core.prediction.Prediction;
import org.lgiavedoni.android.franq.core.prediction.imp.PredictionEventDate;
import org.lgiavedoni.android.franq.core.prediction.rule.PredictionRule;
import org.lgiavedoni.android.franq.util.date.DateUtil;

@Data
public class PredictionRuleEventDay extends PredictionRule {

	private final PredictableEvent event;
	private Set<Integer> daysOfWeek = new HashSet<Integer>();
	// Hour in 24hrs
	private int hourOfDay;
	private int minutes;

	public PredictionRuleEventDay(PredictableEvent event, Set<Integer> days, int hour) {
		super();
		this.event = event;
		this.daysOfWeek = days;
		this.hourOfDay = hour;
	}

	public PredictionRuleEventDay(PredictableEvent event) {
		super();
		this.event = event;
	}

	@Override
	public List<Prediction> generatePredictions() {
		List<Prediction> l = new ArrayList<Prediction>();
		for (int day : daysOfWeek) {
			Calendar predictedDate = Calendar.getInstance();
			predictedDate.set(Calendar.DAY_OF_WEEK, day);
			predictedDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
			predictedDate.set(Calendar.MINUTE, minutes);
			predictedDate.set(Calendar.SECOND, 0);

			Prediction p = new PredictionEventDate(event, predictedDate.getTime());
			l.add(p);
		}
		return l;
	}

	public void addDayOfWeek(int day) {
		daysOfWeek.add(day);
	}

	public PredictableEvent getEvent() {
		return event;
	}

	public String getDaysOfFormated() {
		StringBuffer sb = new StringBuffer();
		for (Integer i : daysOfWeek) {
			sb.append(DateUtil.parseDayOfWeekToString(i) + ", ");
		}
		return sb.toString();
	}

	public String getTimeFormated() {
		return hourOfDay + " hrs. " + minutes + " mins.";
	}

	@Override
	public String toString() {
		return event + "[" + daysOfWeek + "]";
	}

}
